﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Feeds
*/

/***********************************************************************
 Load data for feeds
***********************************************************************/
function loadFeeds(){
    var feeds;
    var parser;
		
  //items = new Array();
	feeds = restoreFeeds();

	if( feeds.length > 0 ) 
	{
		for(var i = 0; i < feeds.length; i++)
		{
		    parser = COMM.sendXMLHttpRequest(feeds[i].url, false, feeds[i].login, feeds[i].user, feeds[i].password);
		    if (parser != null)
		        parser.parseFeed(feeds[i].url);
		}
	}

	var mins = options.getValue( "refreshMinutes" );
	view.setTimeout( "loadFeeds()", mins * 60 * 1000);

	view.setTimeout( "showItems()", 3000);	// 5 Seconds delay for showing

}

/***********************************************************************
  Restore Feed from options values
***********************************************************************/
function restoreFeeds(){
	var store = options.getValue( "feeds" );
	var f = store.split("@@@" );
	var feed;
	var temp;
	var feeds = new Array();

	feedsDefined = false;
	for( var i = 0;i < f.length;i++)
	{
		feed = new Object()
		
		f[i] = f[i] + "@";
		s = f[i].indexOf( "@U=" );
		e = f[i].indexOf( "@", s + 1 );
		temp = f[i].slice( s + 3, e  );
		if ( temp == '' ) 
			break;
		feed.url = temp;

		s = f[i].indexOf( "@N=" );
		e = f[i].indexOf( "@", s + 1 );
		temp = f[i].slice( s + 3, e  );
		if((temp != "undefined" ) && (temp != '' ))
			feed.user = temp;
		s = f[i].indexOf( "@P=" );
		e = f[i].indexOf( "@", s + 1 );
		temp = f[i].slice( s + 3, e  );
		if((temp != "undefined" ) && (temp != '' ))
			feed.password = temp;
		s = f[i].indexOf( "@L=" );
		e = f[i].indexOf( "@", s + 1 );
		temp = f[i].slice( s + 3, e  );
		if (temp != "undefined" ) 
			feed.login = temp
		else
			feed.login = '';
		s = f[i].indexOf( "@R=" );
		e = f[i].indexOf( "@", s + 1 );
		temp = f[i].slice( s + 3, e  );
		if (temp != "undefined" ) 
			feed.lastread = temp
		else
			feed.lastread = 0;
	feed.remove = false;

	feeds.push( feed );
	feedsDefined = true;
	}
	return feeds;
}

/***********************************************************************
 Save Feeds to option Values
***********************************************************************/
function saveFeeds( feedsToSave ){
	var	store = ""

	for( var i = 0; i<feedsToSave.length; i++)
	{
		store = store + "@U=" + feedsToSave[i].url + "@N=" + feedsToSave[i].user + "@P=" + feedsToSave[i].password  + "@L=" + feedsToSave[i].login + "@R=" + feedsToSave[i].lastread +"@@@"
	}

	options.putValue( "feeds", store.substring( 0, store.length - 3 ) );
	options.encryptValue( "feeds" );

}
